package com.wushijia.designpatterns.command;

/**
 * description:
 *
 * @author yang
 * @date 2018/1/19 21:17
 */
public class RemoteControlWithUndo {

  Command[] onCommands;
  Command[] offCommands;
  Command undoCommand;

  public RemoteControlWithUndo() {
    onCommands = new Command[7];
    offCommands = new Command[7];
    Command noCommand = new NoCommand();
    for (int i = 0; i < 7; i++) {
      onCommands[i] = noCommand;
      offCommands[i] = noCommand;
    }
    undoCommand = noCommand;
  }

  public void setCommand(int slot, Command onCommand, Command offCommand) {
    onCommands[slot] = onCommand;
    offCommands[slot] = offCommand;
  }

  public void onButtonWithPressed(int slot) {
    onCommands[slot].execute();
    undoCommand = onCommands[slot];
  }

  public void offButtonWithPressed(int slot) {
    offCommands[slot].undo();
    undoCommand = offCommands[slot];
  }

  public void undoButtonWithPressed() {
    undoCommand.undo();
  }
}
